#ifndef CHATDIALOG_H
#define CHATDIALOG_H

#include <QAction>
#include <QDialog>
#include <QListWidgetItem>
#include <QMouseEvent>
#include <QVBoxLayout>
#include "chatpage.h"
#include "userdata.h"
namespace Ui {
class ChatDialog;
}

class ChatDialog : public QDialog
{
    Q_OBJECT

public:
    explicit ChatDialog(QWidget *parent = nullptr);
    ~ChatDialog();

private:
    Ui::ChatDialog *ui;
    bool _b_loading;
    QMap<int, QListWidgetItem *> _chatUserMap;
    // 存储各个用户的聊天页面
    QMap<int, std::shared_ptr<ChatPage>> _friendPages;
    QVBoxLayout *chat_msg_page_layout;

private slots:
    // 搜索框输入发生变化
    void on_search_edit_textChanged(const QString &arg1);
    // 左侧stacked_widget当前显示页面发生变化
    void on_chatdlg_left_stacked_widget_currentChanged(int arg1);
    // 加载聊天列表
    void slot_loading_chat_user();
    // 添加新的好友申请
    void slot_addFriendApply(std::shared_ptr<AddFriendApply>);
    // 展示好友信息
    void slot_showFriendInfo(std::shared_ptr<FriendInfo>);
    // 跳转好友聊天页面
    void slot_jumpFriendChatPage(std::shared_ptr<FriendInfo>);

    void slot_hasNewTextMessage(std::shared_ptr<TextChatData>);

    void slot_charUserClicked(std::shared_ptr<FriendInfo>);

public:
    virtual bool eventFilter(QObject *watched, QEvent *event) override;

signals:
    void sig_loading_chat_user();

private:
    void addChatUserList();
    void handleGlobalMousePress(QMouseEvent *event);
    QListWidgetItem *findChatUserItem(int);
    void appendChatUserItem(std::shared_ptr<FriendInfo>);
    QListWidgetItem *insertChatUserItem(int, std::shared_ptr<FriendInfo>, bool showRedPoint = false);
    void removeChatUserItem(int);
    QListWidgetItem *makeChatUserFirst(std::shared_ptr<FriendInfo>, bool showRedPoint = false);
    void showChatPage(std::shared_ptr<FriendInfo>);
    void loadUnreceivedMessage();

    // QWidget interface
protected:
    virtual void paintEvent(QPaintEvent *event) override;

    // QWidget interface
protected:
    virtual void mousePressEvent(QMouseEvent *event) override;
};

#endif // CHATDIALOG_H
